/**
 * 
 */
package com.ap.core.business.message.response.translators.impl;

import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.response.translators.ResponseMetaData;
import com.ap.core.business.message.response.translators.ResponseTranslator;
import com.ap.core.business.message.response.translators.listener.ResponseListener;
import com.ap.core.cxf.jaxb.common.ActivationStatus;
import com.ap.core.cxf.jaxb.common.ConsumerStatusEnum;
import com.ap.core.cxf.jaxb.common.YNType;
import com.ap.core.cxf.jaxb.manageconsumer.ConsRegRequestResponseInfoType;
import com.ap.core.dao.ZappConsumerUserDao;
import com.ap.core.dao.ZappUserDao;
import com.ap.core.domain.ZappConsumerUser;
import com.ap.core.domain.ZappUser;
import com.ap.core.exception.BusinessFailureException;
import com.ap.core.message.manageconsumer.info.RequestRetrieveConsumerDetailsTypeInfo;
import com.ap.core.message.manageconsumer.info.ResponseRetrieveConsumerDetailsTypeInfo;

/**
 * The Class ResponseRetrieveConsumerDetailsTranslator.
 *
 * @author Siva.Pochana
 */
public class ResponseRetrieveConsumerDetailsTranslator implements
		ResponseTranslator<RequestRetrieveConsumerDetailsTypeInfo, ResponseRetrieveConsumerDetailsTypeInfo> {

	/** The zapp consumer user dao. */
	private ZappConsumerUserDao zappConsumerUserDao;
	
	/** The zapp user dao. */
	private ZappUserDao zappUserDao;
	 
	/* (non-Javadoc)
	 * @see com.ap.core.business.message.response.translators.ResponseTranslator#populateResponse(java.lang.Object, java.lang.Object, com.ap.core.business.message.response.translators.ResponseMetaData, com.ap.core.business.message.response.translators.listener.ResponseListener)
	 */
	@Override
	public void populateResponse(
			RequestRetrieveConsumerDetailsTypeInfo request,
			ResponseRetrieveConsumerDetailsTypeInfo response,
			BindingMetaData bindingMetaData, ResponseMetaData responseMetaData, ResponseListener responseListener)
			throws BusinessFailureException {
		
		response.setConsRegRequestInfo(request.getConsRegRequestInfo());
		final ConsRegRequestResponseInfoType consRegRequestResponseInfoType = new ConsRegRequestResponseInfoType();
		
		ZappUser zappUser = zappUserDao.getZappUserByEmailId(request.getConsRegRequestInfo().getEmailId());
		consRegRequestResponseInfoType.setFIConsumerFlag(YNType.N);
		
		if(null == zappUser) {
			consRegRequestResponseInfoType.setConsumerStatus(ConsumerStatusEnum.NOTKNOWN);
		} else  {
			if(ActivationStatus.ACTIVE.equals(zappUser.getStatusCode())) {
				consRegRequestResponseInfoType.setConsumerStatus(ConsumerStatusEnum.ACTIVE);
			} else {
				consRegRequestResponseInfoType.setConsumerStatus(ConsumerStatusEnum.INACTIVE);
			}
			consRegRequestResponseInfoType.setMobNb(zappUser.getZappMobileNumber());
			
			ZappConsumerUser zappConsumerUser = zappConsumerUserDao.getZappConsumerUserByEmailIdAndFiId(request.getConsRegRequestInfo().getEmailId(), 
				request.getConsRegRequestInfo().getFIId());
			if(null != zappConsumerUser && !zappConsumerUser.getFiCustomerId().trim().equals(request.getConsRegRequestInfo().getFIConsumerId().trim())) {
				consRegRequestResponseInfoType.setFIConsumerFlag(YNType.Y);
				consRegRequestResponseInfoType.setHeldFIConsumerId(zappConsumerUser.getFiCustomerId());
			}
		}
		
		response.setConsRegRequestResponseInfo(consRegRequestResponseInfoType);
		
		responseListener.onMessageResponse(response);
	}

	/**
	 * Sets the zapp consumer user dao.
	 *
	 * @param zappConsumerUserDao the zappConsumerUserDao to set
	 */
	public void setZappConsumerUserDao(ZappConsumerUserDao zappConsumerUserDao) {
		this.zappConsumerUserDao = zappConsumerUserDao;
	}

	/**
	 * Sets the zapp user dao.
	 *
	 * @param zappUserDao the zappUserDao to set
	 */
	public void setZappUserDao(ZappUserDao zappUserDao) {
		this.zappUserDao = zappUserDao;
	}
}
